package com.example.config.httpclient;

import com.example.pojo.config.WeChatConfig;
import com.example.pojo.http.resp.WeChatUserResp;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 微信获取用户信息接口
 */
@Component
public class WeChatUserClient {
    @Resource
    private RestTemplate restTemplate;
    @Resource
    private WeChatConfig config;

    public WeChatUserResp getGrantUserInfo(String accessToken, String openid) {

        String url = config.getUserInfoApi() + "?access_token={ACCESS_TOKEN}&openid={OPENID}&lang={LANG}";
        Map<String, Object> queryParams = new HashMap<>();
        queryParams.put("ACCESS_TOKEN", accessToken);
        queryParams.put("OPENID", openid);
        queryParams.put("LANG", "zh_CN");

        //请求
        WeChatUserResp weChatUserResp = restTemplate.getForObject(url, WeChatUserResp.class, queryParams);
        return weChatUserResp;
    }
}